function timeoutPromise(promise, timeout) {
    return Promise.race([
        promise,
        new Promise((_, reject) =>
            setTimeout(() => reject(new Error('超时')), timeout)
        )
    ]);
}

// 使用
const apiCall = fetch('/api/data');
timeoutPromise(apiCall, 5000)
    .then(response => console.log('成功'))
    .catch(error => console.log('超时或失败'));